<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            顺引校验
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">车架号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="carSequenceNo" placeholder="请 录入/扫描车序号" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tm">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="mrlCode" placeholder="请 录入/扫描箱码" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="boxnum">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="qty">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotCode">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:5px;"></div>
        <div class="aui-btn aui-btn-info" style="margin-left:5%;width:40%" onclick="check()">校验</div>
        <div class="aui-btn aui-btn-info" style="margin-left:5%;width:40%" onclick="doSearch()">查询</div>
        <div style="background-color:white; height: 270px;width: 100%;margin-top:5px;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                        <td algin='center'>车号</td>
                        <td algin='center'>车序号</td>
                        <td algin='center'>顺序号</td>
                        <td algin='center'>校验标识</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>

        <div style="background-color:white; height: 270px;width: 100%;margin-top:5px;">
            <table class="tableDetails" style='width:100%;' id="tableDetails">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                        <td algin='center'>车型代码</td>
                        <!-- <td algin='center'>底盘号</td> -->
                        <td algin='center'>零件号</td>
                        <td algin='center'>顺序号</td>
                        <td algin='center'>箱号</td>
                        <td algin='center'>校验标识</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtmlDetails">
                </tbody>
            </table>
        </div>
    </div>

    <div>
        <div style="height:50px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var mrlCode = ""; //物料编码
    var boxNum = ""; //箱号

    var scanRule = ""; //条码解析规则
    var arrSave = [];

    //初始化
    apiready = function () {

        idSelector = "carSequenceNo"
        $("#carSequenceNo").select();
        //查询条码解析规则
        initScanRule();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：车序号
            if (idSelector == "carSequenceNo") {

                $("#carSequenceNo").val(ret);

                autoSearch(ret);

                //2:物料号
            } else if (idSelector == "mrlCode") {
                $("#mrlCode").blur();
                $("#tm").val(ret);
                tmSplit(ret);


                // //替换空格和-字符
                // ret = ret.substr(0, ret.length - 10);
                // ret = ret.replace(/-/g, "");
                // ret = ret.replace(/\s+/g, "");
                // $("#mrlCode").val(ret);

                //               autoCheck(ret);
            }
        });
    }


    //自动保存
    function saveAuto(sm) {

        //物料号
        var mrlCode = $("#mrlCode").val();
        //批次号
        var lotCode = $("#lotCode").val();
        //转移数量数量
        var qty = $("#qty").val();
        //条码
        var tm = $("#tm").val();
        //箱号
        var boxnum = $("#boxnum").val();
        //tmID
        var tmID = $("#tmID").val();

        //箱码非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '箱号不能为空',
                duration: 3000
            });
            return;
        }

        //物料非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '物料号不能为空',
                duration: 3000
            });
            return;
        }

        //判断箱号是否重复
        for (var i = 0; i < arrSave.length; i++) {
            if ($("#boxnum").val() == arrSave[i].boxnum) {
                $("#mrlCode").val("");
                $("#mrlCode").select()
                api.toast({
                    msg: '箱号已存在！',
                    duration: 3000
                });
                return;
            }
        }
        var model = {
            mrlCode: mrlCode, //物料号
            lotCode: lotCode, //批次号
            qty: qty, //转移数量
            tm: tm, //条码
            boxnum: boxnum, //箱号
            tmID: tmID //条码ID
        }
        arrSave.push(model);

        //校验
        autoCheck(sm);
    }


    //扫描之后条码解析
    function tmSplit(sm) {
        $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            $("#mrlCode").val(arr[1]);
        }
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            $("#qty").val(arr[2]);
        }
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            $("#lotCode").val(arr[3]);
        }
        if (arr[4] == null || arr[4] == undefined) {
            $("#tmID").val("");
        } else {
            $("#tmID").val(arr[4]);
        }
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            $("#boxnum").val(arr[9]);
        }

        //自动保存
        saveAuto(sm);


    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    //自动保存
                    saveAuto(sm);
                } else if (myobj.code == '1') {

                    $("#mrlCode").val("");
                    $("#mrlCode").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }


    //车序号扫描事件
    $("#carSequenceNo").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            //车序号
            $("#carSequenceNo").val(sm);

            //查询
            autoSearch(sm);
        }
    });

    //物料号扫描事件
    $("#mrlCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            if(tm != null && tm != "" && tm!= undefined){
            $("#mrlCode").blur();
            console.log(tm);
            // $("#boxnum").select();
            //调用条码解析+保存事件
            tmSplit(sm);
  }
           // $("#tm").select();
            //            $("#tm").val(sm);
           

            // //替换空格和-字符为空格
            // sm = sm.substr(0, sm.length - 10);
            // sm = sm.replace(/-/g, "");
            // sm = sm.replace(/\s+/g, "");
            // $("#mrlCode").val(sm);

            //物料号
            // $("#mrlCode").val(sm.substr(0, sm.length - 10));


        }
    });

    //自动查询
    function autoSearch(tm) {
        if (tm != null && tm != "" && tm != undefined) {

            doSearch();
        }
    }

    //自动校验
    function autoCheck(tm) {
        if (tm != null && tm != "" && tm != undefined) {
            var carSequenceNo = $("#carSequenceNo").val();

            //车序号已录入，校验
            if (carSequenceNo != null && carSequenceNo != "" && carSequenceNo != undefined) {
                check();
            }
        }
    }

    //查询
    function doSearch() {
        //车序号
        var carSequenceNo = $("#carSequenceNo").val();

        //车序号不能为空
        if (carSequenceNo == null || carSequenceNo == "" || carSequenceNo == undefined) {
            api.toast({
                msg: "请 录入/扫描车序号",
                duration: 3000
            });
            return;
        }

        //条码特殊字符处理
        // carSequenceNo = encodeURIComponent(encodeURIComponent(carSequenceNo));

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchDeliveryinfoST";
        var data = {
            values: {
                carSequenceNo: carSequenceNo,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    //主表
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                        html += "<td name='deliveryNo'>" + list[i].deliveryNo + "</td>";
                        if (list[i].carSequenceNo == carSequenceNo) {
                            html += "<td name='carSequenceNo' style='background-color:yellow;'>" + list[i].carSequenceNo + "</td>";
                        } else {
                            html += "<td name='carSequenceNo'>" + list[i].carSequenceNo + "</td>";
                        }
                        html += "<td name='seqno'>" + list[i].seqno + "</td>";
                        if (list[i].checkState == "Y") {
                            html += "<td name='checkState' style='background-color:green;'>" + list[i].checkState + "</td>";
                        } else {
                            html += "<td name='checkState' style='background-color:red;'>" + list[i].checkState + "</td>";
                        }
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                    //子表
                    var listDetails = eval(myobj.listDetails);
                    var htmlDetails = "";
                    arrSave = [];
                    for (var i = 0; i < listDetails.length; i++) {
                        var model = { boxnum: listDetails[i].boxnum, }//箱号
                        arrSave.push(model);
                        var boxnumber = listDetails[i].boxnum == null ? "" : listDetails[i].boxnum
                        var color = listDetails[i].color == null ? "" : listDetails[i].color
                        htmlDetails += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                        if (listDetails[i].ext08 == "N") {
                            htmlDetails += "<td name='carmodelsCode' style='background-color:" + color + ";'>" + listDetails[i].carmodelsCode + "</td>";
                            htmlDetails += "<td name='mtlCode' style='background-color:" + color + ";'>" + listDetails[i].mtlCode + "</td>";
                            htmlDetails += "<td name='seqno' style='background-color:" + color + ";'>" + listDetails[i].seqno + "</td>";
                            htmlDetails += "<td name='boxnum' style='background-color:" + color + ";'>" + boxnumber + "</td>";
                        } else {
                            htmlDetails += "<td name='carmodelsCode'>" + listDetails[i].carmodelsCode + "</td>";
                            htmlDetails += "<td name='mtlCode'>" + listDetails[i].mtlCode + "</td>";
                            htmlDetails += "<td name='seqno'>" + listDetails[i].seqno + "</td>";
                            htmlDetails += "<td name='boxnum'>" + boxnumber + "</td>";
                        }
                        if (listDetails[i].checkState == "Y") {
                            htmlDetails += "<td name='checkState' style='background-color:green;'>" + listDetails[i].checkState + "</td>";
                        } else {
                            htmlDetails += "<td name='checkState' style='background-color:red;'>" + listDetails[i].checkState + "</td>";
                        }
                        htmlDetails += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtmlDetails"), htmlDetails);

                    //失败
                } else if (myobj.code == "1") {

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    $api.html($api.byId("tbodyHtmlDetails"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //校验
    function check() {
        //车序号
        var carSequenceNo = $("#carSequenceNo").val();
        //物料号
        var mrlCode = $("#mrlCode").val();
        //箱号
        var boxnum = $("#boxnum").val();
        //箱号
        var qty = $("#qty").val();
        //箱号
        var lotCode = $("#lotCode").val();
        //物料号扫描条码
        var tm = $("#tm").val();



        //车序号非空校验
        if (carSequenceNo == "" || carSequenceNo == null || carSequenceNo == undefined) {
            api.toast({
                msg: '请 录入/扫描车序号',
                duration: 3000
            });
            return;
        }

        //物料号非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '请 录入/扫描物料号',
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '校验中...',
        });

        //特殊字符处理
        // carSequenceNo = encodeURIComponent(encodeURIComponent(carSequenceNo));
        // mrlCode = encodeURIComponent(encodeURIComponent(mrlCode));

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/checkDeliveryinfoMrlST";
        var data = {
            values: {
                carSequenceNo: carSequenceNo, //车序号
                mrlCode: mrlCode, // 物料号
                tm: tm, //条码
                boxnum: boxnum, //箱号
                qty: qty, //箱号
                lotCode: lotCode, //箱号
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "校验成功",
                        duration: 3000
                    });

                    //清空表单
                    $("#mrlCode").val("");
                    $("#tm").val("");
                    $("#mrlCode").focus();
                    idSelector = "mrlCode";
                    //查询
                    doSearch();

                } else if (myobj.code == '1') {

                    api.hideProgress();
                    //弹框提示
                    api.confirm({
                        title: '错误提示',
                        msg: myobj.message,
                        buttons: ['确定', '取消']
                    }, function (ret, err) {
                        var index = ret.buttonIndex;
                        if (index == 1) {

                            $("#mrlCode").val("");
                            $("#tm").val("");
                            idSelector = "mrlCode";
                            $("#mrlCode").select()
                            api.hideProgress();
                            doSearch();
                        } else {
                            $("#mrlCode").select();
                        }
                    });
                }
            }else{
                $("#mrlCode").val("");
                $("#mrlCode").select();
                idSelector = "mrlCode";
                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>